﻿//js获取cookie

function getck(sname) {
    var acookie = document.cookie.split("; ");
    for (var i = 0; i < acookie.length; i++) {

        var arr = acookie[i].split("=");
        if (sname == arr[0]) {
            if (arr.length > 1)
                return unescape(arr[1]);
            else
                return "";
        }
    }
    return "";
}
function setCookie(name, value, expires, domain)//三个参数，一个是cookie的名子，一个是值,另一个是过期时间(单位:分钟)
{
    var Minutes = expires; //
    var exp = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Minutes * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=" + "/" + ";domain=" + domain;
}
function getCookie(name)//取cookies函数        
{
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return null;

}
function delCookie(name, domain)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval == null) {
        cval = "-1";
    }
    document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=" + "/" + ";domain=" + domain;

}

///
//JQuery Cookie插件
///
/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* 
*/
jQuery.cookie = function (key, value, options)
{

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]")
    {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined)
        {
            options.expires = -1;
        }

        if (typeof options.expires === 'number')
        {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};